//
//  MonthChargeViewController.m
//  Runone
//
//  Created by runone02 on 17/4/20.
//  Copyright © 2017年 runone. All rights reserved.
//

#import "MonthChargeViewController.h"
#import "MonthChargeViewCell.h"
#import "GLTollStationDataCell.h"
#import "GLTollStationSectionHeaderView.h"
#import "UIView+Addition.h"
#import "MJRefresh.h"
#import "EKPopMenuView.h"
#import "NSDate+Extension.h"
#import "RequestHelper.h"
#import "RequestGinsengModel.h"
#import "RequestOutginseng.h"
#import "MJExtension.h"
#import "SVProgressHUD.h"
#import "TollStationChargeSummaryModel.h"
#import "TollStationChargeListModel.h"
#import "GLTollStationDataCell.h"
#import "AppConstants.h"
#import "OneConst.h"

//////////////////////
////内网开发专用 登录接口
//#define GetLOGINURL @"http://192.168.1.200:8088/OAuth/Token"
////内网开发专用 API URL
//#define GetAPIURL @"http://192.168.1.200:8090"

#define RGBCOLOR(r,g,b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1]
#define kBtnLayerColor  RGBCOLOR(196, 197, 198)
static NSString *cellID = @"MonthCharge";

@interface MonthChargeViewController () <UITableViewDataSource, UITableViewDelegate>
/** 表格视图 */
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 模型数组 */
@property (strong, nonatomic) NSMutableArray *ModelArray;
@property (weak, nonatomic) IBOutlet UIButton *timeBtn;

@property (nonatomic, strong)GLTollStationSectionHeaderView *stationHeaderV;
@property (nonatomic, strong)EKPopMenuView *dateMenuV;
@property (nonatomic, strong)TollStationChargeSummaryModel *summaryModel;

@property (nonatomic, copy)NSString *yMDStr;
@end

@implementation MonthChargeViewController

#pragma mark - 懒加载
- (NSMutableArray *)ModelArray{
    if (_ModelArray == nil) {
        _ModelArray = [[NSMutableArray alloc] init];
    }
    return _ModelArray;
}

- (void)configTimeBtn {
    [self.timeBtn setBackgroundColor:RGBCOLOR(89, 187, 222)];
    [self.timeBtn setTitleColor:kTextColor forState:UIControlStateNormal
     ];
    self.timeBtn.layer.cornerRadius = 2.0;
}

- (void) setUpDateStr {
    NSArray *tempArr = [_dateString componentsSeparatedByString:@"-"];
    _yMDStr = [NSString stringWithFormat:@"%@年%@月",tempArr[0],tempArr[1]];
    [self.timeBtn setTitle:_yMDStr forState:UIControlStateNormal];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configTimeBtn];
    [self setUpDateStr];
//    _yMDStr = _popMenuDataArr[0];
    _summaryModel = [[TollStationChargeSummaryModel alloc] init];
    self.tabBarController.tabBar.hidden = YES;
    [self setUpNavigationbar ];
    [self setUpTableView];
    // 1. 注册cell
    UINib *nib = [UINib nibWithNibName:@"GLTollStationDataCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:cellID];
    
    // 2. 预估行高
    self.tableView.estimatedRowHeight = 150;
    [self requsetBDMTollEventTypeDefineInfo];
    [self requsetBDMTollEventTypeDefineInfo];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [_dateMenuV hideWithAnimation:NO];
}


//网络请求
- (void) requsetBDMTollEventTypeDefineInfo {
    
    NSMutableDictionary *dic1=[[NSMutableDictionary alloc] init];
    [dic1 setObject:@"TollStationUID" forKey:@"Key"];
    if (self.TollStationUID) {
          [dic1 setObject:self.TollStationUID forKey:@"Value"];
    }
    NSString *yStr = [_yMDStr substringWithRange:NSMakeRange(0, 4)];
    NSString *mStr = [_yMDStr substringWithRange:NSMakeRange(5, 2)];
    
    NSString *dateStr = [NSString stringWithFormat:@"%@-%@",yStr,mStr];
    
    NSMutableDictionary *dic2=[[NSMutableDictionary alloc] init];
    [dic2 setObject:@"ChargeDate" forKey:@"Key"];
    [dic2 setObject:dateStr forKey:@"Value"];
    NSMutableArray *arr=[[NSMutableArray alloc] init];
    [arr addObject:dic1];
    [arr addObject:dic2];
    NSString *json=[arr mj_JSONString];
    
    
    RequestHelper *requestHelper = [[RequestHelper alloc] init];
    RequestGinsengModel *getNewAllHighWayModel = [[RequestGinsengModel alloc] init];
    getNewAllHighWayModel.URL = GetEventDataURL;
    getNewAllHighWayModel.DataValue  = json;
    getNewAllHighWayModel.UpdatedTime  = @"2000-01-01 01:00:00";
    getNewAllHighWayModel.SystemCode = @"GuangleHighway";
    getNewAllHighWayModel.MethodName = @"SearchTollStationChargeByUID";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        if ([requestOutginseng.Code isEqualToString:@"001"]) {
                _summaryModel = [TollStationChargeSummaryModel mj_objectWithKeyValues:requestOutginseng.DataValue];
            [self setUpHeaderViewData];
            }
        
        [self.tableView reloadData];
        [SVProgressHUD dismiss];
    };
    [requestHelper RequestGinsengModel:getNewAllHighWayModel andPushController:self];
}

- (void)setUpHeaderViewData {
    self.totalMoneyLable.text = _summaryModel.TotalCharge;
    self.totalCarNum.text = _summaryModel.TotalTrafficFlow;
}


///设置导航条
- (void) setUpNavigationbar {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 30)];
    label.text = _stationName;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = label;
}
///设置日期
- (void)setUpDefaultValue {
    NSDate *nowDate = [NSDate date];
    NSString *dateStr = [nowDate stringWithFormat:@"yyyy-MM-dd"];
    NSArray *arr = [dateStr componentsSeparatedByString:@"-"];
    NSString *yMonthStr = [NSString stringWithFormat:@"%@年%@月",arr[0],arr[1]];
    [self.stationHeaderV refreshBtnTitleString:yMonthStr];
}

- (IBAction)btnClick:(UIButton *)sender {
    [self setUpPopMenuView];
}

//上下拉刷新
- (void) setUpTableView {
    [self.tableView registerNib:[UINib nibWithNibName:@"GLTollStationDataCell" bundle:nil] forCellReuseIdentifier:cellID];
    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_footer.hidden = YES;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.delegate   = self;
    self.tableView.dataSource = self;
}


- (void) loadNewData {
    [SVProgressHUD showWithStatus:@"正在加载中..."];
    [self requsetBDMTollEventTypeDefineInfo];
    [self.tableView.mj_header endRefreshing];
}

- (void) loadMoreData {
    [self requsetBDMTollEventTypeDefineInfo];
    [self.tableView.mj_footer endRefreshing];// 结束刷新
    self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
     [SVProgressHUD showWithStatus:@"没有更多数据了..."];
 //消除尾部"没有更多数据"的状态
    [_tableView.mj_footer resetNoMoreData];
}


///设置月份菜单
- (void) setUpPopMenuView{
    
    _dateMenuV = [[EKPopMenuView alloc] initEKPopViewWithFrame:CGRectMake(8,CGRectGetMaxY(self.stationHeaderV.frame), 150, 44 * 7) annimationFormLeft:YES];
    
    _dateMenuV.itemNameArray = _popMenuDataArr;
    _dateMenuV.fontSize  = 16.0f;
    _dateMenuV.fontColor = [UIColor whiteColor];
    _dateMenuV.itemBgColor = [UIColor colorWithRed:0.3 green:0.3 blue:0.3 alpha:0.8];
    [_dateMenuV dismissHandler:^(BOOL isCanceled, NSInteger row) {
        self.stationHeaderV.isSelectCanderBtn = NO;
        if (!isCanceled) {
            
            //将选中的月份显示到按钮中
            NSString *dateStr = _popMenuDataArr[row];
            [self.timeBtn setTitle:dateStr forState:UIControlStateNormal];
            
            [self.stationHeaderV refreshBtnTitleString:_popMenuDataArr[row]];
            _yMDStr = _popMenuDataArr[row];
            [self requsetBDMTollEventTypeDefineInfo];
            
            [self.tableView setContentOffset:CGPointMake(0, 0) animated:NO];
            //刷新数据
            [self loadNewData];

        }
    }];
    [_dateMenuV show];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _summaryModel.TollStationChargeList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    // 1. 缓存池中查找
    GLTollStationDataCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.arrowImgV.hidden = YES;
//     2. 传递模型
        TollStationChargeListModel *model = self.summaryModel.TollStationChargeList[indexPath.row];
        cell.stationSumaryM = model;
//
    
    // 3. 返回cell
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}



@end
